<html> <head> <meta http-equiv='Content-Type' content='text/htmlcharset=utf-8' / > <script src='https://code.jquery.com/jquery-1.11.1.min.js'></script> <script src='http://code.highcharts.com/highcharts.js'></script> </head> <body> <div id='chart'></div> <script>
/*
  JavaScript for Stata Highcharts Creator
  Ben Southgate (bsouthga@gmail.com)
  08/20/14
*/

$(document).ready(function() {

  var dataArr = csvToArray(stataHC.data);

  // only keep observations that aren't 
  var data =  [];
  for (var r=0, l=dataArr.length; r<l;r++) {
    var add = true;
    for (var v in dataArr[r]) {
      if (
        dataArr[r][v] === undefined || 
        dataArr[r][v] === ""
      ) {
        add = false;
      }
    }
    if (add) data.push(dataArr[r]);
  }

  var series = genSeriesList(data, stataHC.y_variables);

  var x_val_formatter = function(x) {
    return (stataHC.date ? Highcharts.dateFormat('%a %d %b', x) : x);
  }

  var formatter;
  if (stataHC.y_variables.length > 1 && stataHC.type === "line") {
    formatter = function () {
          var s = '<b>' + stataHC.x_var + " : " + x_val_formatter(this.x) + '</b>';
          $.each(this.points, function () {
              s += '<br/>' + this.series.name + ': ' + this.y;
          });
          return s;
      }
  } else {
    formatter = function () {
          var name = (
            this.name || 
            (this.series ? this.series.name :null ) || 
            (this.points && this.points.series ? this.points.series.name : null )
          );
          return '<b>'+ stataHC.x_var + " : " + x_val_formatter(this.x) + '</b><br/>' + name + ': ' + this.y;
      }    
  }


  $('#chart').highcharts({
    "chart" : {
      "zoomType" : 'xy',
      "type" : stataHC.type
    },
    "title" : {
      "text" : stataHC.title
    },
    "series" : series,
    "yAxis" : {
      "title" : {
        "text" : stataHC.yTitle || (stataHC.y_variables.length === 1 ? stataHC.y_variables[0] : undefined)
      }
    },
    "xAxis": (
      stataHC.date ? 
      {
        type: 'datetime',
        title: { text: stataHC.yTitle || 'Date'}
      } : 
      {
        title: { text: stataHC.yTitle || stataHC.x_var}
      }
    ),
    "tooltip" : {
      "shared" : (stataHC.y_variables.length > 1),
      formatter: formatter
    },
    "plotOptions" : {
      "line" : {
        "marker" : {"enabled" : false}
      }
    }
  });         
})

function commas(number) {
  var negative=(number<0);
  number=Math.abs(number);
  var rounded=Math.round(number),n_str=(''+rounded),ln=n_str.length,output='';
  if ( n_str.indexOf("e") !=- 1) return n_str;
  for ( var i=1;i<=ln;i++)output=(i%3==0&&i!=ln?",":"")+n_str.charAt(ln-i)+output;
  return(negative?'-':'')+output;
}

function genSeriesList(data, y) {
  var series = [];
  for (var i=0, l=y.length; i<l; i++) {
    series.push({
      "name" : y[i],
      "data" : HCSeries(data, stataHC.x_var, y[i])
    });
  }
  return series;
}

function HCSeries(data, x_var, y_var) {
  var out = [];
  var converter = function(v) {
    if (stataHC.date) {
      return new Date(v);
    } else {
      return parseFloat(v);
    }
  };
  for (var i=0, l=data.length;i<l;i++) {
    out.push((function(i) {
      return {
        "x" : converter(data[i][x_var]), 
        "y" : parseFloat(data[i][y_var]), 
        name : (stataHC.byvar ? data[i][stataHC.byvar] : undefined)
      }
    })(i));
  }
  return out.sort(function(a,b) {return (a.x > b.x) - (a.x < b.x)});
}

// CSV string parser to list of objects
// ref: http://stackoverflow.com/a/1293163/2343
function csvToArray( strData, strDelimiter ){
  strDelimiter = (strDelimiter || ",");
  var objPattern = new RegExp(
    (
      // Delimiters.
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
      // Quoted fields.
      "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
      // Standard fields.
      "([^\"\\" + strDelimiter + "\\r\\n]*))"
    ),
    "gi"
    );
  var arrData = [[]];
  var arrMatches = null;
  while (arrMatches = objPattern.exec( strData )){
    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];
    // Check to see if the given delimiter has a length
    // (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know
    // that this delimiter is a row delimiter.
    if (
      strMatchedDelimiter.length &&
      strMatchedDelimiter !== strDelimiter
      ){
      // Since we have reached a new row of data,
      // add an empty row to our data array.
      arrData.push( [] );
    }
    var strMatchedValue;
    // Now that we have our delimiter out of the way,
    // let's check to see which kind of value we
    // captured (quoted or unquoted).
    if (arrMatches[ 2 ]){
      // We found a quoted value. When we capture
      // this value, unescape any double quotes.
      strMatchedValue = arrMatches[ 2 ].replace(
        new RegExp( "\"\"", "g" ),
        "\""
        );

    } else {
      // We found a non-quoted value.
      strMatchedValue = arrMatches[ 3 ];
    }
    // Now that we have our value string, let's add
    // it to the data array.
    arrData[ arrData.length - 1 ].push( strMatchedValue );
  }
  var headers = arrData[0];
  var output = [];
  for (var i=1,l=arrData.length;i<l;i++) {
    var row = {};
    for (var j=0,m=headers.length;j<m;j++) {
      row[headers[j]] = arrData[i][j];
    }
    output.push(row)
  }
  // Return the parsed data.
  return( output );
}



var stataHC = { 'title' : 'Stata Output', 'y_variables' : [  'open' , 'close' , 'low' , 'high'], 'x_var' : 'date', 'type' : 'line', 'byvar' : '', 'date' : 1, 'xTitle' : '', 'yTitle' : '' }
stataHC.data = 'date,open,close,low,high\n02jan2001,1320.28,1283.27,1276.05,1320.28\n03jan2001,1283.27,1347.56,1274.62,1347.76\n04jan2001,1347.56,1333.34,1329.14,1350.24\n05jan2001,1333.34,1298.35,1294.95,1334.77\n08jan2001,1298.35,1295.86,1276.29,1298.35\n09jan2001,1295.86,1300.8,1295.14,1311.72\n10jan2001,1300.8,1313.27,1287.28,1313.76\n11jan2001,1313.27,1326.82,1309.72,1332.19\n12jan2001,1326.82,1318.55,1311.59,1333.21\n16jan2001,1318.32,1326.65,1313.33,1327.81\n17jan2001,1326.65,1329.47,1325.41,1346.92\n18jan2001,1329.89,1347.97,1327.41,1352.71\n19jan2001,1347.97,1342.54,1336.74,1354.55\n22jan2001,1342.54,1342.9,1333.84,1353.62\n23jan2001,1342.9,1360.4,1339.63,1362.9\n24jan2001,1360.4,1364.3,1357.28,1369.75\n25jan2001,1364.3,1357.51,1354.63,1367.35\n26jan2001,1357.51,1354.95,1342.75,1357.51\n29jan2001,1354.92,1364.17,1350.36,1365.54\n30jan2001,1364.17,1373.73,1356.2,1375.68\n31jan2001,1373.73,1366.01,1364.66,1383.37\n01feb2001,1366.01,1373.47,1359.34,1373.5\n02feb2001,1373.47,1349.47,1348.72,1376.38\n05feb2001,1349.47,1354.31,1344.48,1354.56\n06feb2001,1354.31,1352.26,1350.04,1363.55\n07feb2001,1352.26,1340.89,1334.26,1352.26\n08feb2001,1341.1,1332.53,1332.42,1350.32\n09feb2001,1332.53,1314.76,1309.98,1332.53\n12feb2001,1314.76,1330.31,1313.64,1330.96\n13feb2001,1330.31,1318.8,1317.51,1336.62\n14feb2001,1318.8,1315.92,1304.72,1320.73\n15feb2001,1315.92,1326.61,1315.92,1331.29\n16feb2001,1326.61,1301.53,1293.18,1326.61\n20feb2001,1301.53,1278.94,1278.44,1307.16\n21feb2001,1278.94,1255.27,1253.16,1282.97\n22feb2001,1255.27,1252.82,1228.33,1259.94\n23feb2001,1252.82,1245.86,1215.44,1252.82\n26feb2001,1245.86,1267.65,1241.71,1267.69\n27feb2001,1267.65,1257.94,1252.26,1272.76\n28feb2001,1257.94,1239.94,1229.65,1263.47\n01mar2001,1239.94,1241.23,1214.5,1241.36\n02mar2001,1241.23,1234.18,1219.74,1251.01\n05mar2001,1234.18,1241.41,1234.04,1242.55\n06mar2001,1241.41,1253.8,1241.41,1267.42\n07mar2001,1253.8,1261.89,1253.8,1263.86\n08mar2001,1261.89,1264.74,1257.6,1266.5\n09mar2001,1264.74,1233.42,1228.42,1264.74\n12mar2001,1233.42,1180.16,1176.78,1233.42\n13mar2001,1180.16,1197.66,1171.5,1197.83\n14mar2001,1197.66,1166.71,1155.35,1197.66\n15mar2001,1166.71,1173.56,1166.71,1182.04\n16mar2001,1173.56,1150.53,1148.64,1173.56\n19mar2001,1150.53,1170.81,1147.18,1173.5\n20mar2001,1170.81,1142.62,1142.19,1180.56\n21mar2001,1142.62,1122.14,1118.74,1149.39\n22mar2001,1122.14,1117.58,1081.19,1124.27\n23mar2001,1117.58,1139.83,1117.58,1141.83\n26mar2001,1139.83,1152.69,1139.83,1160.02\n27mar2001,1152.69,1182.17,1150.96,1183.35\n28mar2001,1182.17,1153.29,1147.83,1182.17\n29mar2001,1153.29,1147.95,1136.26,1161.69\n30mar2001,1147.95,1160.33,1143.83,1162.8\n02apr2001,1160.33,1145.87,1137.51,1169.51\n03apr2001,1145.87,1106.46,1100.19,1145.87\n04apr2001,1106.46,1103.25,1091.99,1117.5\n05apr2001,1103.25,1151.44,1103.25,1151.47\n06apr2001,1151.44,1128.43,1119.29,1151.44\n09apr2001,1128.43,1137.59,1126.38,1146.13\n10apr2001,1137.59,1168.38,1137.59,1173.92\n11apr2001,1168.38,1165.89,1160.26,1182.24\n12apr2001,1165.89,1183.5,1157.73,1183.51\n16apr2001,1183.5,1179.68,1167.38,1184.64\n17apr2001,1179.68,1191.81,1168.9,1192.25\n18apr2001,1191.81,1238.16,1191.81,1248.42\n19apr2001,1238.16,1253.69,1233.39,1253.71\n20apr2001,1253.7,1242.98,1234.41,1253.7\n23apr2001,1242.98,1224.36,1217.47,1242.98\n24apr2001,1224.36,1209.47,1208.89,1233.54\n25apr2001,1209.47,1228.75,1207.38,1232.36\n26apr2001,1228.75,1234.52,1228.75,1248.3\n27apr2001,1234.52,1253.05,1234.52,1253.07\n30apr2001,1253.05,1249.46,1243.99,1269.3\n01may2001,1249.46,1266.44,1243.55,1266.47\n02may2001,1266.44,1267.43,1257.7,1272.93\n03may2001,1267.43,1248.58,1239.88,1267.43\n04may2001,1248.58,1266.61,1232,1267.51\n07may2001,1266.61,1263.51,1259.19,1270\n08may2001,1266.71,1261.2,1253,1267.01\n09may2001,1261.2,1255.54,1247.83,1261.65\n10may2001,1255.54,1255.18,1254.56,1268.14\n11may2001,1255.18,1245.67,1240.79,1259.84\n14may2001,1245.67,1248.92,1241.02,1249.68\n15may2001,1248.92,1249.44,1245.36,1257.45\n16may2001,1249.44,1284.99,1243.02,1286.39\n17may2001,1284.99,1288.49,1282.65,1296.48\n18may2001,1288.49,1291.96,1281.15,1292.06\n21may2001,1291.96,1312.83,1287.87,1312.95\n22may2001,1312.83,1309.38,1306.89,1315.93\n23may2001,1309.38,1289.05,1288.7,1309.38\n24may2001,1289.05,1293.17,1281.22,1295.04\n25may2001,1293.17,1277.89,1276.42,1293.17\n29may2001,1277.89,1267.93,1265.41,1278.42\n30may2001,1267.93,1248.08,1245.96,1267.93\n31may2001,1248.08,1255.82,1248.07,1261.91\n01jun2001,1255.82,1260.67,1246.88,1265.34\n04jun2001,1260.67,1267.11,1256.36,1267.17\n05jun2001,1267.11,1283.57,1267.11,1286.62\n06jun2001,1283.57,1270.03,1269.01,1283.85\n07jun2001,1270.03,1276.96,1265.08,1277.08\n08jun2001,1276.96,1264.96,1259.99,1277.11\n11jun2001,1264.96,1254.39,1249.23,1264.96\n12jun2001,1254.39,1255.85,1235.75,1261\n13jun2001,1255.85,1241.6,1241.59,1259.75\n14jun2001,1241.6,1219.87,1218.9,1241.6\n15jun2001,1219.87,1214.36,1203.03,1221.5\n18jun2001,1214.36,1208.43,1208.33,1221.23\n19jun2001,1208.43,1212.58,1207.71,1226.11\n20jun2001,1212.58,1223.14,1210.07,1225.61\n21jun2001,1223.14,1237.04,1220.25,1240.24\n22jun2001,1237.04,1225.35,1221.41,1237.73\n25jun2001,1225.35,1218.6,1213.6,1231.5\n26jun2001,1218.6,1216.76,1204.64,1220.7\n27jun2001,1216.76,1211.07,1207.29,1219.92\n28jun2001,1211.07,1226.2,1211.07,1234.44\n29jun2001,1226.2,1224.38,1221.14,1237.29\n02jul2001,1224.42,1236.72,1224.03,1239.78\n03jul2001,1236.71,1234.45,1229.43,1236.71\n05jul2001,1234.45,1219.24,1219.15,1234.45\n06jul2001,1219.24,1190.59,1188.74,1219.24\n09jul2001,1190.59,1198.78,1189.75,1201.76\n10jul2001,1198.78,1181.52,1179.93,1203.43\n11jul2001,1181.52,1180.18,1168.46,1184.93\n12jul2001,1180.18,1208.14,1180.18,1210.25\n13jul2001,1208.14,1215.68,1203.61,1218.54\n16jul2001,1215.68,1202.45,1200.05,1219.63\n17jul2001,1202.45,1214.44,1196.14,1215.36\n18jul2001,1214.44,1207.71,1198.33,1214.44\n19jul2001,1207.71,1215.02,1205.8,1225.04\n20jul2001,1215.02,1210.85,1207.04,1215.69\n23jul2001,1210.85,1191.03,1190.5,1215.22\n24jul2001,1191.03,1171.65,1165.54,1191.03\n25jul2001,1171.65,1190.49,1171.28,1190.52\n26jul2001,1190.49,1202.93,1182.65,1204.18\n27jul2001,1202.93,1205.82,1195.99,1209.26\n30jul2001,1205.82,1204.52,1200.41,1209.05\n31jul2001,1204.52,1211.23,1204.52,1222.74\n01aug2001,1211.23,1215.93,1211.23,1223.04\n02aug2001,1215.93,1220.75,1215.31,1226.27\n03aug2001,1220.75,1214.35,1205.31,1220.75\n06aug2001,1214.35,1200.48,1197.35,1214.35\n07aug2001,1200.47,1204.4,1195.64,1207.56\n08aug2001,1204.4,1183.53,1181.27,1206.79\n09aug2001,1183.53,1183.43,1174.68,1184.71\n10aug2001,1183.43,1190.16,1169.55,1193.33\n13aug2001,1190.16,1191.29,1185.12,1193.82\n14aug2001,1191.29,1186.73,1184.26,1198.79\n15aug2001,1186.73,1178.02,1177.61,1191.21\n16aug2001,1178.02,1181.66,1166.08,1181.8\n17aug2001,1181.66,1161.97,1156.07,1181.66\n20aug2001,1161.97,1171.41,1160.94,1171.41\n21aug2001,1171.41,1157.26,1156.56,1179.85\n22aug2001,1157.26,1165.31,1153.34,1168.56\n23aug2001,1165.31,1162.09,1160.96,1169.86\n24aug2001,1162.09,1184.93,1162.09,1185.15\n27aug2001,1184.93,1179.21,1178.07,1186.85\n28aug2001,1179.21,1161.51,1161.17,1179.66\n29aug2001,1161.51,1148.56,1147.38,1166.97\n30aug2001,1148.6,1129.03,1124.87,1151.75\n31aug2001,1129.03,1133.58,1126.38,1141.83\n04sep2001,1133.58,1132.94,1129.06,1155.4\n05sep2001,1132.94,1131.74,1114.86,1135.52\n06sep2001,1131.74,1106.4,1105.83,1131.74\n07sep2001,1106.4,1085.78,1082.12,1106.4\n10sep2001,1085.78,1092.54,1073.15,1096.94\n17sep2001,1092.54,1038.77,1037.46,1092.54\n18sep2001,1038.77,1032.74,1029.25,1046.42\n19sep2001,1032.74,1016.1,984.62,1038.91\n20sep2001,1016.1,984.54,984.49,1016.1\n21sep2001,984.54,965.8,944.75,984.54\n24sep2001,965.8,1003.45,965.8,1008.44\n25sep2001,1003.45,1012.27,998.33,1017.14\n26sep2001,1012.27,1007.04,1002.62,1020.29\n27sep2001,1007.04,1018.61,998.24,1018.92\n28sep2001,1018.61,1040.94,1018.61,1040.94\n01oct2001,1040.94,1038.55,1026.76,1040.94\n02oct2001,1038.55,1051.33,1034.47,1051.33\n03oct2001,1051.33,1072.28,1041.48,1075.38\n04oct2001,1072.28,1069.63,1067.82,1084.12\n05oct2001,1069.62,1071.38,1053.5,1072.35\n08oct2001,1071.37,1062.44,1056.88,1071.37\n09oct2001,1062.44,1056.75,1053.83,1063.37\n10oct2001,1056.75,1080.99,1052.76,1081.62\n11oct2001,1080.99,1097.43,1080.99,1099.16\n12oct2001,1097.43,1091.65,1072.15,1097.43\n15oct2001,1091.65,1089.98,1078.19,1091.65\n16oct2001,1089.98,1097.54,1087.13,1101.66\n17oct2001,1097.54,1077.09,1076.57,1107.12\n18oct2001,1077.09,1068.61,1064.54,1077.94\n19oct2001,1068.61,1073.48,1057.24,1075.52\n22oct2001,1073.48,1089.9,1070.79,1090.57\n23oct2001,1089.9,1084.78,1081.53,1098.99\n24oct2001,1084.78,1085.2,1079.98,1090.26\n25oct2001,1085.2,1100.09,1065.64,1100.09\n26oct2001,1100.09,1104.61,1094.24,1110.61\n29oct2001,1104.61,1078.3,1078.3,1104.61\n30oct2001,1078.3,1059.79,1053.61,1078.3\n31oct2001,1059.79,1059.78,1057.55,1074.79\n01nov2001,1059.78,1084.1,1054.31,1085.61\n02nov2001,1084.1,1087.2,1075.58,1089.63\n05nov2001,1087.2,1102.84,1087.2,1106.72\n06nov2001,1102.84,1118.86,1095.36,1119.73\n07nov2001,1118.86,1115.8,1112.98,1126.62\n08nov2001,1115.8,1118.54,1115.42,1135.75\n09nov2001,1118.54,1120.31,1111.13,1123.02\n12nov2001,1120.31,1118.33,1098.32,1121.71\n13nov2001,1118.33,1139.09,1118.33,1139.14\n14nov2001,1139.09,1141.21,1132.87,1148.28\n15nov2001,1141.21,1142.24,1135.06,1146.46\n16nov2001,1142.24,1138.65,1129.92,1143.52\n19nov2001,1138.65,1151.06,1138.65,1151.06\n20nov2001,1151.06,1142.66,1142.17,1152.45\n21nov2001,1142.66,1137.03,1129.78,1142.66\n23nov2001,1137.03,1150.34,1135.9,1151.05\n26nov2001,1150.34,1157.42,1146.17,1157.88\n27nov2001,1157.42,1149.5,1140.81,1163.38\n28nov2001,1149.5,1128.52,1128.29,1149.5\n29nov2001,1128.52,1140.2,1125.51,1140.4\n30nov2001,1140.2,1139.45,1135.89,1143.57\n03dec2001,1139.45,1129.9,1125.78,1139.45\n04dec2001,1129.9,1144.8,1128.86,1144.8\n05dec2001,1143.77,1170.35,1143.77,1173.62\n06dec2001,1170.35,1167.1,1164.43,1173.35\n07dec2001,1167.1,1158.31,1152.66,1167.1\n10dec2001,1158.31,1139.93,1139.66,1158.31\n11dec2001,1139.93,1136.76,1134.32,1150.89\n12dec2001,1136.76,1137.07,1126.01,1141.58\n13dec2001,1137.07,1119.38,1117.85,1137.07\n14dec2001,1119.38,1123.09,1114.53,1128.28\n17dec2001,1123.09,1134.36,1122.66,1137.3\n18dec2001,1134.36,1142.92,1134.36,1145.1\n19dec2001,1142.92,1149.56,1134.75,1152.44\n20dec2001,1149.56,1139.93,1139.93,1151.42\n21dec2001,1139.93,1144.89,1139.93,1147.46\n24dec2001,1144.89,1144.65,1144.62,1147.83\n26dec2001,1144.65,1149.37,1144.65,1159.18\n27dec2001,1149.37,1157.13,1149.37,1157.13\n28dec2001,1157.13,1161.02,1157.13,1164.64\n31dec2001,1161.02,1148.08,1148.04,1161.16\n';</script></body></html>